package com.yonyou.pmclouds.safeinspection.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.procedure.entity.ProcedureVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionListVO;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionVO;
import com.yonyou.pmclouds.safeinspection.mapper.SafeinspectionMapper;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionQueryService;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolDetailQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/4/21   14:02
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = SafeinspectionQueryService.class)
public class SafeinspectionQueryServiceImpl implements SafeinspectionQueryService {

    @Resource
    private SafeinspectionMapper querymapper;
    @Resource
    private SafetypatrolQuery safetypatrolQuery;
    @Resource
    private PictureRemoteService pictureRemoteService;
    @Resource
    private InspectItemService inspectItemService;
    @Resource
    private ProjectSimpleQuery projectSimpleQuery;
    //这个是检查工序的
    @Resource
    private ProcedureQuery procedureQuery;
    @Resource
    private OrganizationQuery organizationQuery;
    @Resource
    private SafetypatrolDetailQuery safetypatrolDetailQuery;

    //给列表态的检查标准赋值
    @Override
    public  List<SafeInspectionListVO> queryByPkProject(String pk_project) throws BusinessException {
        List<SafeInspectionListVO> vos = querymapper.selectByProjectId(pk_project);

        if (vos!=null){
            //或者安全标准的主键
            List<String> pklist =new ArrayList<>();
            //不能用增强for循环 这里要用下标赋值
            for (SafeInspectionListVO listVO : vos) {
                pklist.add(listVO.getPk_inspectstandard());
            }
            //这个是新方法 头一次见
            for (String pk : pklist) {
                SafetypatrolVO safetypatrolVO = safetypatrolQuery.queryById(pk);
                for (int i = 0; i <vos.size() ; i++) {
                    vos.get(i).setInspectareaName(safetypatrolVO.getStandardName());
                }
            }

            //图片这边先不添加
        }
        return vos;
    }




    //这边只能写在一个接口中 因为页面还要展示通过不通过
    @Override
    public SafeInspectionVO queryByOne(String pk_safeinspection) throws BusinessException {
        if (pk_safeinspection==null){
            return null;
        }
        SafeInspectionVO inspectionVO = querymapper.getById(pk_safeinspection);
        if (inspectionVO!=null){
            SafetypatrolVO safetypatrolVO = safetypatrolQuery.queryById(inspectionVO.getPk_safetypatrol());
            List<SafetypatrolDetailVO> detailVOList = safetypatrolDetailQuery.queryById(safetypatrolVO.getPkSafetypatrol());
            InspectItemVO[] inspectItemVOS = inspectItemService.queryByPkbill(inspectionVO.getPk_safetypatrol());
            inspectionVO.setInspectItemVOS(inspectItemVOS);

        }
        return fillOtherFields(inspectionVO);

    }



    //查看现场检查记录
    @Override
    public InspectItemVO queryItemVo(String pk_inspectitem) throws BusinessException {
        if (StringUtils.isEmpty(pk_inspectitem)){
            return null;
        }
        InspectItemVO inspectItemVO = inspectItemService.queryOne(pk_inspectitem);
        if (inspectItemVO!=null){
            return inspectItemVO;
        }
        return null;
    }


    //这个方法里面没加施工单位 需要再加
    private SafeInspectionVO fillOtherFields(SafeInspectionVO vo) throws BusinessException {
        String pk_project =vo.getPk_project();
        //填充项目名称
        ProjectVO projectVO = projectSimpleQuery.queryProject(pk_project);
        if (projectVO!=null){
            vo.setProjectName(projectVO.getSname());
            vo.setProjectCode(projectVO.getScode());
            vo.setManager(projectVO.getManager());
            //这个app看一下 现在进不去
            //现在一般都用这个方法 带租户id的 这个组织主键在项目实体类中
            //为了测试先把这个注释掉 TODO
//            OrganizationVO organizationVO = organizationQuery.queryByPkSrc(projectVO.getPkTenant(), projectVO.getPkSrc());
//            if (organizationVO!=null){
//                //打印预览需要
//                vo.setOrgName(organizationVO.getSname());
//            }

        }
        //填充工序
        ProcedureVO procedureVO = procedureQuery.getById(vo.getPk_process());
        if (procedureVO!=null){
            vo.setProcessName(procedureVO.getSname());
        }
        return vo;
    }

}
